<template>
  <div class="yy-whp-supplier-manage">
    <!--列表-->
    <div class="yy-supplier-manage-list" v-show="type=='list'">
      <Card>
        <p slot="title">
          <Icon type="md-list" />
          供应商列表
        </p>
        <Form ref="formInline" :model="searchForm"  inline>
            <FormItem prop="user">
              <Input type="text" v-model="searchForm.supName" placeholder="供应商名称"></Input>
            </FormItem>
            <FormItem>
              <Button type="primary" @click="handleSearch()">查询</Button>
            </FormItem>
            <FormItem>
              <Button type="primary" @click="handleAdd()">新增</Button>
            </FormItem>
        </Form>
        <Table :columns="columns" :data="data" ></Table>
        <div style="margin: 10px;overflow: hidden">
          <div style="float: right;">
              <Page :total="page.total" :page-size="page.pageSize" :current="1" @on-change="changePage" show-total></Page>
          </div>
        </div>
      </Card>
    </div>
    <!--详情-->
    <div class="yy-supplier-manage-details" v-show="type=='details'">
       <Card>
        <p slot="title">
          <Icon type="md-paper"/>
          查看详情
        </p>
        <p slot="extra">
          <Button type="primary" @click="goBack()">返回</Button>
        </p>
        <div class="details">
          <p class="details-title"><span>供应商编号:</span>{{currentData.supNum}}</p>
          <p class="details-title"><span>供应商名称:</span>{{currentData.supName}}</p>
          <p class="details-title"><span>联系人:</span>{{currentData.linkPerson}}</p>
          <p class="details-title"><span>电话:</span>{{currentData.phone}}</p>
          <p class="details-title"><span>传真:</span>{{currentData.fax}}</p>
          <p class="details-title"><span>电子邮件:</span>{{currentData.email}}</p>
          <p class="details-title"><span>发票名称:</span>{{currentData.invoiceName}}</p>
        </div>
      </Card>
    </div>
    <!--添加/编辑-->
    <div class="yy-supplier-manage-add-edit"  v-show="(type=='add' || type=='edit')">
      <Card>
        <p slot="title">
          <template v-if="type=='add'"> 
            <Icon type="md-create"/> 新增
          </template>
          <template v-if="type=='edit'"> 
            <Icon type="md-document"/>编辑
          </template>
        </p>
        <p slot="extra" >
          <Button type="primary" @click="goBack()">返回</Button>
        </p>
        <div class="editForm">
          <Form ref="editForm" :model="editFormInLine" :rules="editFormRuleInline" :label-width="100" @keydown.native.enter.prevent ="handleSubmit">
             <FormItem label="供应商编号" prop="supNum">
              <Input v-model="editFormInLine.supNum" placeholder="请输入供应商编号" ></Input>
            </FormItem>
            <FormItem label="供应商名称"  prop="supName">
              <Input v-model="editFormInLine.supName" placeholder="请输入供应商名称"></Input>
            </FormItem>
            <FormItem label="联系人"  prop="linkPerson">
              <Input v-model="editFormInLine.linkPerson" placeholder="请输入联系人"></Input>
            </FormItem>
            <FormItem label="电话"  prop="phone">
              <Input v-model="editFormInLine.phone" placeholder="电话"></Input>
            </FormItem>
            <FormItem label="传真"  prop="fax">
              <Input v-model="editFormInLine.fax" placeholder="传真"></Input>
            </FormItem>
            <FormItem label="电子邮件"  prop="email">
              <Input v-model="editFormInLine.email" placeholder="电子邮件"></Input>
            </FormItem>
            <FormItem label="发票名称"  prop="invoiceName">
              <Input v-model="editFormInLine.invoiceName" placeholder="发票名称"></Input>
            </FormItem>
            <FormItem label="纳税人识别号"  prop="b_DepartNum">
              <Input v-model="editFormInLine.b_DepartNum" placeholder="纳税人识别号"></Input>
            </FormItem>
            <FormItem label="供货商地址"  prop="address">
              <Input v-model="editFormInLine.address" placeholder="供货商地址"></Input>
            </FormItem>
            <FormItem label="备注" prop="description">
              <Input v-model="editFormInLine.description" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="备注" maxlength="50"></Input>
            </FormItem>
            <FormItem>
              <Button @click="handleSubmit" type="primary" class="button">提交</Button>
              <Button @click="handleReset" type="primary" class="button">重置</Button>
            </FormItem>
          </Form>
        </div>
      </Card>
    </div>
  </div>
</template>

<script>
import API from "./api";
import ValidatorRule from "@/filters/validator-rule";
import { formatNetTime } from "@/libs/tools";
import i18n from "./locale";

export default {
  name: "yy-whp-supplier-manage",
  data() {
    return {
      i18n: i18n,
      currentData: "",
      detailsModal: false,
      type: "list",
      page: {
        total: 0,
        pageSize: 20
      },
      searchForm: {
        supName: ""
      },
      editFormInLine: {
        id: "",
        supNum: "",
        supName: "",
        linkPerson: "",
        phone: "",
        fax: "",
        email: "",
        invoiceName: "",
        invoiceNum: "",
        address: "",
        description: "",
        userName: "",
        userID: ""
      },
      editFormRuleInline: {
        supNum: ValidatorRule.require,
        supName: ValidatorRule.require
      },
      columns: [
        {
          title: "供应商编号",
          key: "supNum"
        },
        {
          title: "供应商名称",
          key: "supName"
        },
        {
          title: "联系人 ",
          key: "linkPerson"
        },
        {
          title: "电话 ",
          key: "phone"
        },
        {
          title: "传真 ",
          key: "fax"
        },
        {
          title: "电子邮件 ",
          key: "email"
        },
        {
          title: "发票名称 ",
          key: "invoiceName"
        },
        {
          title: "操作",
          key: "action",
          width: 180,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.details(params.index);
                    }
                  }
                },
                "查看"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.edit(params.index);
                    }
                  }
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      this.delete(params.index, params.row.id);
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        }
      ],
      data: []
    };
  },

  mounted() {
    this.userInfo = this.$store.state.user.userInfo;
    this.handleSearch();
  },

  computed: {},

  methods: {
    handleSearch() {
      let obj = this.searchForm;
      obj.pageNum = 1;
      obj.pageSize = this.page.pageSize;
      this.searchForm = obj;
      this.getList(obj);
    },

    getList(obj) {
      API.getList(obj)
        .then(res => {
          if (res.code == 200) {
            let data = res.data;
            this.page.total = data.total;
            this.page.pageSize = data.pageSize;
            this.data = data.list;
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    changePage(pageNum) {
      let obj = this.searchForm,
        maxPageNum = Math.ceil(this.page.total / this.page.pageSize);
      if (obj.pageNum > maxPageNum) {
        obj.pageNum = maxPageNum;
      } else {
        obj.pageNum = pageNum;
      }
      this.searchForm = obj;
      this.getList(obj);
    },

    goBack() {
      this.type = "list";
      console.log(276, this.data);
    },

    handleAdd() {
      this.type = "add";
      this.handleReset();
    },

    details(index) {
      this.type = "details";
      this.currentData = this.data[index];
    },

    edit(index) {
      this.handleReset();
      this.type = "edit";
      this.currentData = this.data[index];
      this.editFormInLine = _.clone(this.currentData);
    },

    delete(index, id) {
      let tthis = this;
      this.$Modal.confirm({
        title: "提示：",
        content: "你确定删除这条记录",
        onOk: function() {
          let obj = {
            ID: id,
            UserID: tthis.userInfo.userId
          };
          API.delete(obj)
            .then(res => {
              if (res.code == 200) {
                this.$Message.success({ content: "该记录已经删除成功！" });
                tthis.data.splice(index, 1);
                tthis.resetList();
              }
            })
            .catch(err => {
              console.log(err);
              this.$Message.error("请求异常！");
            });
        }
      });
    },

    resetList() {
      //求余为1，页码为最大页码，请求页需减1
      let maxPageNum = Math.ceil(this.page.total / this.page.pageSize);
      let remainderNum = this.page.total % this.page.pageSize;
      if (
        this.searchForm.pageNum == maxPageNum &&
        remainderNum == 1 &&
        this.searchForm.pageNum !== 1
      ) {
        this.searchForm.pageNum = this.searchForm.pageNum - 1;
      }
      let obj = this.searchForm;
      this.getList(obj);
    },

    handleSubmit() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          if (this.type == "edit") {
            this.editSubmit(this.editFormInLine);
          } else {
            this.addSubmit(this.editFormInLine);
          }
        }
      });
    },

    handleReset() {
      this.$refs.editForm.resetFields();
    },

    editSubmit(obj) {
      obj.id = this.currentData.id;
      API.editSubmit(obj)
        .then(res => {
          if (res.code == 200) {
            this.resetList();
            this.$Message.success("修改成功！");
            this.goBack();
          }
        })
        .catch(err => {
          this.$Message.error("修改异常！");
        });
    },

    addSubmit(obj) {
      API.addSubmit(obj)
        .then(res => {
          if (res.code == 200) {
            this.handleSearch();
            this.$Message.success("添加成功！");
            this.goBack();
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    }
  }
};
</script>
<style lang="less" >
@import "./yy-whp-supplier-manage.less";
</style>
